package com.briup.web;

import com.briup.pojo.Book;
import com.briup.utils.Result;
import com.briup.utils.ResultCode;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {
    //根据id去查询图书
    //映射配置
    //127.0.0.1:8081/books/getById/1
    //@RequestMapping(value="/getById/{id}",method = RequestMethod.GET)
    //简化的配置
    //普通接口
    //@GetMapping("/getById/{id}")
    //优点：隐藏资源的访问行为，无法通过URL获取对资源的操作方法
    //书写简化
    //ERESTFul风格接口
    @GetMapping("/{id}")//127.0.0.1:8081/books/1(具体操作根据选择来 GET DELETE)
    public Result getById(@PathVariable Integer id) {
        System.out.println("getById" + id);
        Book book = new Book();
        book.setId(id);
        book.setName("java核心讲义");
        //生成一个异常
    int n = id / 0;
        //无统一标准的返回
//        return book;
//        return
//                {
//                    code:200,
//        message:"success,书籍查询成功"
//                data：book,
//                }
//
//模拟失败请求
        if (id == 1) {
            return Result.failure(ResultCode.DATA_NONE);
        }
//        模拟成功请求
        return Result.success(book);
    }
    //get post put delect
    //127.0.0.1:8081/books/delById/1
    //@DeleteMapping("/delById/{id}")
    @DeleteMapping("/{id}")
    public  Result delById(@PathVariable Integer id) {
        System.out.println("delById" + id);
        return Result.success();
//            return "book"+id+"删除成功";
//            return
//                    {
//                            code:200,
//                    message:"success,书籍删除成功",
//            data：null,
//                }
    }

}
